package InventoryAssetReturnDetail

import (
	"ToolLibray/jwtx"
	"context"
	"property_api/internal/svc"
	"property_api/internal/types"
	"property_api/utils/result"
	"property_api/utils/xform"
	"property_service/client/inventory_service"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddLogic {
	return &AddLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// Add 添加资产退库明细的逻辑
func (l *AddLogic) Add(req *types.InventoryAssetReturnDetail) (resp *types.Response, err error) {
	// 设置租户ID，从上下文中获取
	tenantID := jwtx.GetUserTenantId64(l.ctx)
	// 设置创建用户和更新用户，从上下文中获取
	userIdStr := xform.Int64ToString(jwtx.GetUserId(l.ctx))

	// 调用服务层方法，添加资产退库明细记录
	res, err := l.svcCtx.InventoryService.InventoryAssetReturnDetailAdd(l.ctx, &inventory_service.InventoryAssetReturnDetail{
		ReturnID:          req.ReturnID,          // 退库单ID
		AssetID:           req.AssetID,           // 资产ID
		AssetCode:         req.AssetCode,         // 资产编号
		AssetName:         req.AssetName,         // 资产名称
		Category:          req.Category,          // 分类
		CategoryID:        req.CategoryID,        // 分类ID
		SpecModel:         req.SpecModel,         // 规格型号
		Brand:             req.Brand,             // 品牌
		BrandID:           req.BrandID,           // 品牌ID
		Origin:            req.Origin,            // 产地
		OriginID:          req.OriginID,          // 产地ID
		Unit:              req.Unit,              // 单位
		UnitID:            req.UnitID,            // 单位ID
		UnitPrice:         req.UnitPrice,         // 单价
		UnitPriceID:       req.UnitPriceID,       // 单价ID
		Quantity:          req.Quantity,          // 数量
		StorageLocation:   req.StorageLocation,   // 存放地点
		StorageLocationID: req.StorageLocationID, // 存放地点ID
		Manufacturer:      req.Manufacturer,      // 生产商
		ManufacturerID:    req.ManufacturerID,    // 生产商ID
		TenantID:          tenantID,              // 租户ID
		CreatedAtuser:     userIdStr,             // 创建人
		UpdatedAtuser:     userIdStr,             // 更新人
	})

	// 如果服务层返回错误，返回统一的错误响应
	if err != nil {
		return result.Err(err).End()
	}

	// 返回成功的响应
	return result.Suc(res).End()
}
